package dao

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"strconv"
	"time"
)

type Image struct {
	Uid int `json:"uid" gorm:"column:uid"`
	ImgId int `json:"img_id" gorm:"column:img_id"`
	Uuid string `json:"uuid" gorm:"column:uuid"`
	BackupStorageUuid string `json:"backupStorageUuid" gorm:"column:backupStorageUuid"`
	Name string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description"`
	Username string `json:"username" gorm:"column:username"`
	Password string `json:"password" gorm:"column:password"`
	VirType string `json:"vir_type" gorm:"column:vir_type"`
	State string `json:"state" gorm:"column:state"`
	Status string `json:"status" gorm:"column:status"`
	Size float64 `json:"size" gorm:"column:size"`
	ActualSize float64 `json:"actualSize" gorm:"column:actualSize"`
	Md5Sum string `json:"md5Sum" gorm:"column:md5Sum"`
	Url string `json:"url" gorm:"column:url"`
	MediaType string `json:"mediaType" gorm:"column:mediaType"`
	Type string `json:"type" gorm:"column:type"`
	Platform string `json:"platform" gorm:"column:platform"`
	Format string `json:"format" gorm:"column:format"`
	System string `json:"system" gorm:"column:system"`
	InstallPath string `json:"installPath" gorm:"column:installPath"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
}
func (t *Image)Create(d map[string]interface{}) error{

	var img Image
	if v,ok := d["uid"];ok{
		fmt.Println(v.(string))
		int, _ := strconv.Atoi(v.(string))
		img.Uid = int
	}
	if v,ok := d["uuid"];ok{
		img.Uuid = v.(string)
	}
	if v,ok := d["backupStorageUuid"];ok{
		img.BackupStorageUuid = v.(string)
	}
	if v,ok := d["name"];ok{
		img.Name = v.(string)
	}
	if v,ok := d["description"];ok{
		img.Description = v.(string)
	}
	if v,ok := d["username"];ok{
		img.Username = v.(string)
	}
	if v,ok := d["password"];ok{
		img.Password = v.(string)
	}
	if v,ok := d["vir_type"];ok{
		img.VirType = v.(string)
	}
	if v,ok := d["state"];ok{
		img.State = v.(string)
	}
	if v,ok := d["status"];ok{
		img.Status = v.(string)
	}
	if v,ok := d["size"];ok{
		img.Size = v.(float64)
	}
	if v,ok := d["actualSize"];ok{
		img.ActualSize = v.(float64)
	}
	if v,ok := d["md5Sum"];ok{
		img.Md5Sum = v.(string)
	}
	if v,ok := d["url"];ok{
		img.Url = v.(string)
	}
	if v,ok := d["mediaType"];ok{
		img.MediaType = v.(string)
	}
	if v,ok := d["type"];ok{
		img.Type = v.(string)
	}
	if v,ok := d["platform"];ok{
		img.Platform = v.(string)
	}
	if v,ok := d["format"];ok{
		img.Format = v.(string)
	}
	if v,ok := d["system"];ok{
		img.System = v.(string)
	}
	if v,ok := d["installPath"];ok{
		img.InstallPath = v.(string)
	}
	img.Ctime = time.Now().Unix()

	err := db.Create(&img).Error
	return err
}
/**
	软删除
 */
func (t *Image)Delete(w map[string]interface{})bool{

	upd := Image{
		Dtime:time.Now().Unix(),
	}
	err := db.Model(&t).Where(w).Update(upd).Error
	b := true
	if err != nil {
		b = false
	}
	return b

}
func (t *Image)Update(uuid string,d map[string]interface{}) bool {

	var img Image
	/*
	if v,ok := d["backupStorageUuid"];ok{
		img.BackupStorageUuid = v.(string)
	}*/

	if v,ok := d["name"];ok{
		img.Name = v.(string)
	}
	if v,ok := d["description"];ok{
		img.Description = v.(string)
	}
	if v,ok := d["username"];ok{
		img.Username = v.(string)
	}
	if v,ok := d["password"];ok{
		img.Password = v.(string)
	}
	if v,ok := d["vir_type"];ok{
		img.VirType = v.(string)
	}
	if v,ok := d["state"];ok{
		img.State = v.(string)
	}
	if v,ok := d["status"];ok{
		img.Status = v.(string)
	}
	if v,ok := d["size"];ok{
		img.Size = v.(float64)
	}
	if v,ok := d["actualSize"];ok{
		img.ActualSize = v.(float64)
	}
	if v,ok := d["md5Sum"];ok{
		img.Md5Sum = v.(string)
	}
	if v,ok := d["url"];ok{
		img.Url = v.(string)
	}
	if v,ok := d["mediaType"];ok{
		img.MediaType = v.(string)
	}
	if v,ok := d["type"];ok{
		img.Type = v.(string)
	}
	if v,ok := d["platform"];ok{
		img.Platform = v.(string)
	}
	if v,ok := d["format"];ok{
		img.Format = v.(string)
	}
	if v,ok := d["system"];ok{
		system := strconv.FormatBool(v.(bool))
		img.System = system
	}
	if v,ok := d["installPath"];ok{
		img.InstallPath = v.(string)
	}

	err := db.Model(&t).Where("uuid = ?",uuid).Updates(d).Error
	b := true
	if err != nil{
		b = false
	}
	return b

}
func (t *Image)Find(w map[string]interface{},page int,pageSize int) ([]*Image,int,error) {

	var total int = 0
	var rs []*Image
	// 获取取指page，指定pagesize的记录
	err := db.Where(w).
		Where("d_time = ?",0).
		Limit(pageSize).Offset((page-1)*pageSize).
		Order("img_id desc").
		Find(&rs).Error

	// 获取总条数
	db.Model(&t).Where(w).Where("d_time = ?",0).Count(&total)

	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}
	return rs,total,nil

}
func (t *Image)First(w map[string]interface{})(bool, *Image){

	err := db.Where(w).First(&t).Error
	if err != nil{
		return false,nil
	}
	if t.ImgId > 0{
		return true,t
	}
	return false,nil

}
var ImageDao = &Image{}
